<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        height: 30px;
      }
      .box input {
        height: 30px;
        width: 300px;
        font-size: 20px;
      }

      .box img {
        width: 20px;
        position: relative;
        left: -35px;
        cursor: pointer;
        vertical-align: middle;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <input type="password" name="password" /><img
        src="eye-slash.png"
        id="showPass"
      />
    </div>

    <script src="jquery/jquery-3.6.1.js"></script>

    <script>
      let btn = $("#showPass");
      let password = $("[name=password]");

      //   btn.on("click", () => {
      //     if (btn.attr("src") == "eye.png") {
      //       // 目前是显示密码的
      //       btn.attr("src", "eye-slash.png");
      //       password.attr("type", "password");
      //     } else {
      //       btn.attr("src", "eye.png");
      //       password.attr("type", "text");
      //     }
      //   });

      btn.hover(
        () => {
          btn.attr("src", "eye.png");
          password.attr("type", "text");
        },
        () => {
          btn.attr("src", "eye-slash.png");
          password.attr("type", "password");
        }
      );
    </script>
  </body>
</html>
